package com.indusfo.spc.client.service.impl;

import java.util.List;

import com.indusfo.spc.service.RuleService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.indusfo.spc.client.mapper.ClientBatchMapper;
import com.indusfo.spc.client.mapper.ClientProMapper;
import com.indusfo.spc.client.mapper.ClientSpcDeteDetailMapper;
import com.indusfo.spc.client.mapper.ClientSpcDeteMapper;
import com.indusfo.spc.client.mapper.ClientSpcOrderMapper;
import com.indusfo.spc.client.pojo.ClientSpcDete;
import com.indusfo.spc.client.pojo.ClientSpcOrder;
import com.indusfo.spc.client.pojo.ClientSpcTest;
import com.indusfo.spc.client.pojo.ClientSpcTestDete;
import com.indusfo.spc.client.service.ClientSpcOrderService;
import com.indusfo.spc.exception.BusinessException;
import com.indusfo.spc.exception.ParamsErrorException;
import com.indusfo.spc.pojo.Batch;
import com.indusfo.spc.pojo.OrderFlow;
import com.indusfo.spc.service.BarCodeRules;

import javax.annotation.Resource;

/**
 * 客户端检测单
 * 
 * @author zhengd
 *
 */
@Service
public class ClientSpcOrderServiceImpl implements ClientSpcOrderService {
	@Resource
	private ClientSpcOrderMapper clientSpcOrderMapper;
	@Resource
	private ClientSpcDeteMapper clientSpcDeteMapper;
	@Resource
	private BarCodeRules barCodeRules;
	@Resource
	private ClientBatchMapper clientBatchMapper;
	@Resource
	private ClientSpcDeteDetailMapper clientSpcDeteDetailMapper;
	@Resource
	private ClientProMapper clientProMapper;
	@Resource
	private RuleService ruleService;

	/**
	 * 客户端检测单新增
	 * 
	 */
	@Override
	@Transactional
	public Integer saveClientSpcOrder(ClientSpcOrder clientSpcOrder) {
		if (null != clientSpcOrder.getBatchCode() && clientSpcOrder.getBatchCode() != "") {
			Batch batch = clientBatchMapper.selectByBatchCode(clientSpcOrder.getBatchCode());
			if (null == batch) {
				throw new ParamsErrorException("输入的批号不存在！");
			}
			clientSpcOrder.setBatchId(batch.getBatchId());
		}
		// 生成不良单编号
		List<String> calculate = ruleService.otherRule(30, 1);
		if (calculate.isEmpty()) {
			throw new BusinessException("生成不良单编号失败！");
		}
		clientSpcOrder.setSpcOrderCode(calculate.get(0));
		// 保存检测单
		Integer count = clientSpcOrderMapper.saveClientSpcOrder(clientSpcOrder);
		if (count == 0) {
			throw new BusinessException("新增检测单失败！");
		}
		// 如果有检测项目集合则进行新增
		if (clientSpcOrder.getClientSpcDetes().size() > 0) {
			insertClientSpcDete(clientSpcOrder, true);
		}
		return count;
	}

	/**
	 * 客户端检测单更新
	 */
	@Override
	@Transactional
	public Integer updateClientSpcOrder(ClientSpcOrder clientSpcOrder) {
		if (null != clientSpcOrder.getBatchCode()) {
			Batch batch = clientBatchMapper.selectByBatchCode(clientSpcOrder.getBatchCode());
			if (null == batch.getBatchId()) {
				throw new ParamsErrorException("批号错误！");
			}
			clientSpcOrder.setBatchId(batch.getBatchId());
		}
		// 先把老数据删掉
		clientSpcDeteMapper.deleteClientSpcDete(clientSpcOrder.getSpcOrderId());
		// 如果有检测项目集合则进行新增
		if (clientSpcOrder.getClientSpcDetes() != null && clientSpcOrder.getClientSpcDetes().size() > 0) {
			insertClientSpcDete(clientSpcOrder, false);
		}
		return clientSpcOrderMapper.updateClientSpcOrder(clientSpcOrder);
	}

	/**
	 * 客户端检测单高级查询
	 */
	@Override
	public List<ClientSpcOrder> queryClientSpcOrder(ClientSpcOrder clientSpcOrder) {
		// 获取一页显示多少行
		Integer pagesize = clientSpcOrder.getPagesize();
		// 获取查询第几页
		Integer pageindex = clientSpcOrder.getPageindex();
		// 若不为空计算从第几条开始查询
		if (pagesize != null && pageindex != null) {
			clientSpcOrder.setIncept(pagesize * (pageindex - 1));
		} else {
			throw new ParamsErrorException("请传入分页参数！");
		}
		return clientSpcOrderMapper.queryClientSpcOrder(clientSpcOrder);
	}

	/**
	 * 客户端检测单高级查询记录数
	 */
	@Override
	public Integer countQueryClientSpcOrder(ClientSpcOrder clientSpcOrder) {
		return clientSpcOrderMapper.countQueryClientSpcOrder(clientSpcOrder);
	}

	/**
	 * 根据任务单ID和工序ID查询流程ID
	 */
	@Override
	public Integer queryOrderFlowIdByOrderId(OrderFlow orderFlow) {
		return clientSpcOrderMapper.queryOrderFlowIdByOrderId(orderFlow);
	}

	/**
	 * 根据检测单ID查询检测项目
	 */
	@Override
	public List<ClientSpcDete> queryClientSpcDete(Integer spcOrderId) {
		// 查询检测项目信息
		List<ClientSpcDete> clientSpcDetes = clientSpcDeteMapper.queryClientSpcDete(spcOrderId);
		return clientSpcDetes;
	}

	/**
	 * 根据检测单ID查询基本信息
	 */
	@Override
	public ClientSpcTest queryClientSpcTest(Integer spcOrderId) {
		// 查询检测单基本信息
		ClientSpcTest clientSpcTest = clientSpcOrderMapper.queryClientSpcTest(spcOrderId);
		// 查询检测项目信息
		List<ClientSpcTestDete> clientSpcTestDetes = clientSpcDeteMapper.queryClientSpcTestDete(spcOrderId);
		if (!clientSpcTestDetes.isEmpty()) {
			// 循环查询检测值
			for (ClientSpcTestDete clientSpcTestDete : clientSpcTestDetes) {
				// 查询每组平均值
				clientSpcTestDete.setAverageValues(
						clientSpcDeteDetailMapper.queryAverageDeteValueBySpcDeteId(clientSpcTestDete.getSpcDeteId()));
				// 查询前25组值
				clientSpcTestDete.setAllValues(clientSpcDeteDetailMapper
						.queryTwentyFiveGroupDeteValueBySpcDeteId(clientSpcTestDete.getSpcDeteId()));
			}
		}
		clientSpcTest.setClientSpcTestDetes(clientSpcTestDetes);
		return clientSpcTest;
	}

	/**
	 * 新增检测项目
	 * 
	 * @param ClientSpcOrder
	 */
	private void insertClientSpcDete(ClientSpcOrder clientSpcOrder, Boolean insert) {
		for (ClientSpcDete clientSpcDete : clientSpcOrder.getClientSpcDetes()) {
			if (clientSpcDete.getDeteId() == null) {
				throw new ParamsErrorException("检测项目ID不能为空！");
			}
			if (clientSpcDete.getUpperLimit() == null) {
				throw new ParamsErrorException("检测上限不能为空！");
			}
			if (clientSpcDete.getLowerLimit() == null) {
				throw new ParamsErrorException("检测下限不能为空！");
			}
			if (clientSpcDete.getMiddleLimit() == null) {
				throw new ParamsErrorException("检测中心值不能为空！");
			}
			if (clientSpcDete.getSerialNum() == null) {
				throw new ParamsErrorException("检测序号不能为空！");
			}
			if (clientSpcDete.getSampleNum() == null) {
				throw new ParamsErrorException("检测分组不能为空！");
			}
			if (clientSpcDete.getSampleCount() == null) {
				throw new ParamsErrorException("每组数量不能为空！");
			}
			// 设置检测单ID
			clientSpcDete.setSpcOrderId(clientSpcOrder.getSpcOrderId());
		}
		Integer count = clientSpcDeteMapper.saveClientSpcDete(clientSpcOrder.getClientSpcDetes());
		if (count != clientSpcOrder.getClientSpcDetes().size() && insert) {
			throw new BusinessException("新增检测项目失败！");
		} else if (count != clientSpcOrder.getClientSpcDetes().size() && !insert) {
			throw new BusinessException("更新检测项目失败！");
		}
	}

	/**
	 * 根据任务单ID查询工序
	 */
	@Override
	public List<OrderFlow> selectProByOrderId(Integer orderId) {
		return clientProMapper.selectProByOrderId(orderId);
	}

	/**
	 * 客户端检测单完成
	 */
	@Override
	@Transactional
	public String finishClientSpcOrder(ClientSpcOrder clientSpcOrder) {
		clientSpcOrder.setDeteState(3);
		clientSpcOrderMapper.updateClientSpcOrder(clientSpcOrder);
		return "完成检测单成功！";
	}
}
